package com.hula.onion.book.server.spider.platforms.ppmoney;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by work on 2018/1/18.
 */
@Component("ppmoneySpider")
public class PPmoneySpider extends SpiderCommon {
    @Autowired
    private PPmoneySpiderHelper spiderHelper;

    private static final String LOGIN_URL = "https://www.ppmoney.com/login/";

    private static final String GET_ACCOUNT_INFO_URL = "https://www.ppmoney.com/Home/InitUser";

    @Override
    public CommonResponse execute() {
        BasicHeader bh = new BasicHeader("Cookie", "_pk_id.1.fd42=;_pk_ses.1.fd42=*");
        String loginStr = HttpUtils.post(LOGIN_URL, spiderHelper.installRequestParams(loginForm), getHttpContext(), bh);
        String initUserStr = HttpUtils.post(GET_ACCOUNT_INFO_URL, null, getHttpContext());
        if (StringUtils.isEmpty(initUserStr)) {
            return new CommonResponse(ResponseStatusEnum.FAIL);
        }
        return spiderHelper.parseAccountBalance(initUserStr);
    }
}
